<?php
/**
 * User: hjd-yao
 * Date: 2019/6/4
 * Time: 14:21
 */

namespace Shop\Http\Controllers;


use Illuminate\Http\Request;
use App\Models\Shop\ShopAdInfoModel;
use App\Models\Shop\ShopGoodsBillModel;
use App\Models\Shop\ShopGoodsModel;
use Frontend\Models\UserBaseModel;
use App\Models\Cms\NoticeModel;
use App\Models\Cms\ArticleModel;
use App\Models\Shop\ShopCategoryModel;
use App\Models\Shop\AdPosModel;
use App\Models\Shop\ShopCouponModel;

class IndexController extends BaseController
{
    public function index(Request $request)
    {
        if(is_mobile()){
            return $this->wapIndex($request);
        }else{
            return $this->wapIndex($request);
        }
    }

    private function pcIndex()
    {
        $category = ShopCategoryModel::where('status',1)->select(['id','name','name_mobile','parent_id'])->get()->toArray();
        $category = list_to_tree($category,true,'id','parent_id');
        $category = $this->categoryGoods($category);

        $this->getAd();

        return view('PShop::index.index',[
            'page_title'=>'首页',
            'category'=>$category
        ]);
    }

    private function categoryGoods($category)
    {
        foreach ($category as &$item){
            $goods = ShopGoodsModel::where('status',1)->whereRaw("FIND_IN_SET(?,`category_ids`)",[$item['id']])
                ->select(['goods_id','goods_name','title','description','price','vip_price','goods_images'])
                ->limit(12)->get()->toArray();
            $item['goods'] = array_chunk($goods,6);
        }
        unset($item);
        return $category;
    }

    public function getAd()
    {
        $slides = AdPosModel::with('ad')->whereHas('ad',function($query){
            $query->where('status',1);
        })->where('pos_code','AD21264823')->where('status',1)->first();//轮播图

        $slide = $slides&&$slides->ad?$slides->ad:[];

        view()->share('slides',$slide);

        $slides_down = AdPosModel::with('ad')->whereHas('ad',function($query){
            $query->where('status',1);
        })->where('pos_code','AD21264824')->where('status',1)->first();//中间轮播图

        $down = $slides_down&&$slides_down->ad?$slides_down->ad:[];

        view()->share('slide_down',$down);

        $top_ad = AdPosModel::with('ad')->whereHas('ad',function($query){
            $query->where('status',1);
        })->where('pos_code','AD51177956')->where('status',1)->first();//首页顶部广告

        $top = $top_ad&&$top_ad->ad?$top_ad->ad:[];
        view()->share('top_ad',$top);

        //优惠券

//        $list = ShopCouponModel::where('status',1)->whereIn('store_id',[-1,0])->whereRaw('release_number>used_number')->limit(5)->get();
//
//        view()->share('coupons',$list);
    }

    private function wapIndex(Request $request)
    {
        $user_id = $request->input('user_id');
        $can_cj_times=0;
        if($user_id){
            $user=UserBaseModel::where('member_id',$user_id)->first();
            $can_cj_times=$user->can_cj_times;
        }

        $slides = ShopAdInfoModel::where('type',2)->where('status',1)->select(['img_url','url'])->get();

        $goods = ShopGoodsModel::where('is_home_show',1)->where('status',1)->inRandomOrder()->forPage(1,20)->orderBy('sort','desc')->get();

        $notice_into= NoticeModel::init()->where('is_open',1)->where('type',1)->select('id', 'title')->orderBy('created_at','desc')->first();

        $article_infos = ArticleModel::init()->where('is_open',100)->select('id', 'title')->orderBy('created_at','desc')->take(5)->get();

        return view('WShop::index.index',[
            'page_title'=>'首页',
            'slides'=>$slides,
            'goods'=>$goods,
            'notice_into'=>$notice_into,
            'article_infos'=>$article_infos,
            'remind'=>0,
            'can_cj_times'=>$can_cj_times,
        ]);
    }






}